<template>
	<view>
		<view class="u-m-t-20">
			<u-cell-group>
				<u-cell-item  title="是否推送上课信息" :arrow="false" class="info">
						<u-switch slot="right-icon" v-model="isSubscribe" :loading="switchLoading" @change="switchChange"></u-switch>
				</u-cell-item>
			</u-cell-group>
		</view>
		<button class="buttonCss" v-show="isSubscribe == true" @click="showSwitchWeek = true">推送倒计时：{{oldMin}}分钟
		<u-icon style="position: relative;left: 10rpx;" name="arrow-down"></u-icon></button>
		
		<view>
			<u-select v-model="showSwitchWeek" :list="list" :default-value="[defaultlocation]"  @confirm="confirmMins()"></u-select>
		</view>
		<br>
	<p v-show="isSubscribe == true" class="text">我们将以该推送倒计时为准，在公众号“仲园服务平台”</p>
	<p v-show="isSubscribe == true" class="text">提前向您推送上课信息，请确保您已关注公众号！</p>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				isShowSuc:true,
				defaultlocation:0,
				oldMin:0,
				newMin:0,
				showSwitchWeek: false,
				isSubscribe: true,
				header: '',
				switchLoading: false,
				isConcern: false,
				list: [
					{value: '5',label: '5分钟',extra: '5'},
					{value: '10',label: '10分钟',extra: '10'},
					{value: '15',label: '15分钟',extra: '15'},
					{value: '20',label: '20分钟',extra: '20'},
					{value: '25',label: '25分钟',extra: '25'},
					{value: '30',label: '30分钟',extra: '30'},
					{value: '35',label: '35分钟',extra: '35'},
					{value: '40',label: '40分钟',extra: '40'}
				],
			}
		},
		async onLoad() {
			let header = new Object()
			let authority = uni.getStorageSync("Authority")
			header.Authority = authority
			this.header = header
			// let isConcern = getApp().globalData.isConcern
			console.log(getApp().globalData.subscribe)
			// if (isConcern == true && getApp().globalData.subscribe == true){
			let isOpen = await getApp().UniRequest("/user/authority","GET","",header)
			console.log('@@@@@@@@@@@@@@@@@@@@')
			if (isOpen.code == 20000){
				this.isSubscribe = true
			}else{
				this.isSubscribe = false
			}
			console.log(isOpen)
			let data = await getApp().officailRequest("/officialUser/authority/minutes","GET","",header)
			this.oldMin = data.data.minutes
			this.defaultlocation = this.oldMin/5 - 1
		},
		async onShow() {
			// let vm = this
			// let data = await getApp().UniRequest("/user/authority/confirmOfficial","GET","",vm.header)
			// console.log(data)
			// if (data.code == 20000){
			// 	vm.isConcern = true
			// 	getApp().globalData.isConcern = true
			// }else {
			// 	vm.isConcern = false
			// 	vm.isSubscribe = false
			// 	getApp().globalData.isConcern = false
			// 	getApp().globalData.subscribe = false
			// }
		},
		methods: {
			async confirmMins(res){
				let vm = this
				let body = new Object();
				console.log(res[0].value)
				console.log(this.oldMin)
				body.newMinutes = res[0].value;
				body.oldMinutes = this.oldMin;
				let data = await getApp().officailRequest("/officialUser/authority/change","POST",body,vm.header)
				console.log(data)
				if(data.code == 20000){
					uni.showToast({
						title: '修改成功!',
					})
					this.oldMin = res[0].value
				}else {
					uni.showToast({
						title: '设置失败!',
						icon: 'error'
					})
				}
			},
			async switchChange(){
				let vm = this
				vm.switchLoading = true
				let body = new Object()
				if (vm.isSubscribe == true){
					let res = await uni.showModal({
						title: '为了保证上课提醒的推送，请确保您已关注公众号“仲园服务平台”',
						cancelText:"已关注",
						confirmText:"去关注"
					})
					if (res[1].cancel== true){
						vm.isSubscribe = true
						vm.switchLoading = false
						vm.isShowSuc = true
					}else if (res[1].confirm == true){
						vm.isSubscribe = true
						getApp().globalData.subscribe = true
						uni.navigateTo({
							url: '../attentionOfficial/attentionOfficial'
						})
						vm.isShowSuc = false
					}
					body.subscribe = 1
					getApp().globalData.subscribe = true
				}else {
					let res = await uni.showModal({
						title: '若关闭此功能，您将享受不了每日课程推送服务'
					})
					console.log(res)
					if (res[1].cancel== true){
						vm.isSubscribe = true
						vm.switchLoading = false
						vm.isShowSuc = true
						return
					}
					vm.isShowSuc = true
					body.subscribe = 0;
					getApp().globalData.subscribe = false
				}
				let data = await getApp().UniRequest("/user/authority/setting","POST",body,vm.header)
				let dataTwo = await getApp().officailRequest("/officialUser/authority/setting","POST",body,vm.header)
				if (data.code == 20000 && vm.isShowSuc == true){
					uni.showToast({
						title: '设置成功!',
					})
				}else if(data.code != 20000 && vm.isShowSuc == true){
					uni.showToast({
						title: '设置失败!',
						icon: 'error'
					})
				}
				vm.switchLoading = false
			}
		}
	}
</script>

<style>
.buttonCss{
	color: #000000;
}
.text{
	text-align: center;
	color: #c0c4cc;
	position: relative;
	top: 20rpx;
}
</style>
